package com.zmytest.annotation;

import com.zmytest.dto.MyValidInputDto;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created on 2018/12/21.
 *
 * @author zhangmingyu
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MyInputValid.MyInputValidator.class)
public @interface MyInputValid {

    String message() default "msg 必须为 zmytest，numb必须为200";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class MyInputValidator implements ConstraintValidator<MyInputValid,MyValidInputDto>{

        @Override
        public void initialize(MyInputValid myInputValid) {

        }

        @Override
        public boolean isValid(MyValidInputDto myValidInputDto, ConstraintValidatorContext constraintValidatorContext) {
            return "zmytest".equals(myValidInputDto.getMsg()) && myValidInputDto.getNumb() ==200;
        }
    }
}
